package br.gov.sjc.adapter;

import java.util.List;

import br.gov.sjc.aluguelsocial.R.layout;
import br.gov.sjc.aluguelsocial.R.telefone;
import br.gov.sjc.classes.Telefone;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TelefoneAdapter extends BaseAdapter {
	
	private final String TAG = "TelefoneAdapter";
	private Context context;
	private List<Telefone> lstTelefone;
	private LayoutInflater TelefoneInflater;
	
	public TelefoneAdapter (Context context, List<Telefone> listTelefone){
		this.context = context;
		this.lstTelefone = listTelefone;
		TelefoneInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public void notifyDataSetChanged(){
		try{
			super.notifyDataSetChanged();
		}catch(Exception e){
			Log.e(TAG,"notifyDataSetChanged E - "+e.getMessage());
		}
	}

	public int getCount() {
		return lstTelefone.size();
	}
	
	public void remove(final Telefone item){
		this.lstTelefone.remove(item);
	}
	
	public void add(final Telefone item){
		this.lstTelefone.add(item);
	}

	public Object getItem(int position) {
		return lstTelefone.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup viewGroup) {
		try{
			Telefone telefonevo = lstTelefone.get(position);
			ViewHolder holder;
			if(convertView == null){
				convertView = TelefoneInflater.inflate(layout.telefonetw, null);
				holder = new ViewHolder();
				
				holder.tvTelefone = (TextView) convertView.findViewById(telefone.tvTelefone);
                holder.tvDDD = (TextView) convertView.findViewById(telefone.tvDDD);
                holder.spTipo = (TextView) convertView.findViewById(telefone.tvTipo);
                holder.spUtilizacao = (TextView) convertView.findViewById(telefone.tvUtilizacao);
                holder.tvContato= (TextView) convertView.findViewById(telefone.tvContato);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
            // * COLOCA DADOS NA LISTVIEW
			
			holder.tvTelefone.setText(telefonevo.getTelefone());
            holder.tvDDD.setText(telefonevo.getDDD());
            holder.spTipo.setText(telefonevo.getTipoTelefone());
            holder.spUtilizacao.setText(telefonevo.getTipoUtilizacao());
            holder.tvContato.setText(telefonevo.getContato());
			return convertView;
		}catch(Exception e){
			Log.e(TAG, "getView e - "+e.getMessage());
		}
		return convertView;
	}
    static class ViewHolder {
    	//campos
        public TextView tvTelefone;
        public TextView tvDDD;
        public TextView spTipo;
        public TextView spUtilizacao;
        public TextView tvContato;
        
    }  
}
